<template>
	<view>
		<view class="username" @click="changeAddress">
			<view>修改地址</view>
			<view>{{ address }}</view>
		</view>

		<view class="btn">
			<button @click="loginOut()">退出</button>
			<button @click="save()">保存</button>
		</view>
		<!-- 修改昵称 -->
	</view>
</template>

<script>
export default {
	data() {
		return {
			userinfo: {},
			fileList1: [],
			avatar: '',
			nickname: '',
			information: '',
			address: '',
			title: '修改昵称',
			show: false,
			fileList: [],
			lat: '',
			lon: ''
		};
	},
	onLoad() {
		this.getInfo();
	},
	methods: {
		// 获取茶艺师的地址数据
		changeAddress() {
			console.log('修改地址');
			let self = this;
			uni.chooseLocation({
				type: 'wgs84',
				success: function(res) {
					console.log(res);
					self.lon = res.longitude;
					self.lat = res.latitude;
					self.address = res.address;
					console.log(res.address, self.address);
				}
			});
		},
		// 上传图片
		// 删除图片
		deletePic(event) {
			this[`fileList${event.name}`].splice(event.index, 1);
			console.log(this.fileList1);
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file);
			let fileListLen = this[`fileList${event.name}`].length;
			lists.map(item => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				});
			});
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url);
				let item = this[`fileList${event.name}`][fileListLen];
				this[`fileList${event.name}`].splice(
					fileListLen,
					1,
					Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					})
				);
				fileListLen++;
			}
			// console.log(this.fileList1);
		},
		uploadFilePromise(url) {
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: 'https://chazhichashi.rchz.top/api/newupload/postImage', // 仅为示例，非真实的接口地址
					filePath: url,
					name: 'file',
					formData: {
						user: 'test'
					},
					success: res => {
						setTimeout(() => {
							resolve(res.data.data);
							console.log(JSON.parse(res.data));
							let fileList = [];
							this.fileList.push({
								url: JSON.parse(res.data).data
							});
							let str = '';
							console.log(this.fileList.length);
							for (let i = 0; i <= this.fileList.length - 1; i++) {
								str += this.fileList[i].url + ',';
							}
							if (str.length > 0) {
								str = str.substr(0, str.length - 1);
							}
							console.log(str, '1111111');
							this.information = str;
						}, 1000);
					}
				});
			});
		},
		save() {
			this.https('/order/updateUserInfo', {
				avatar: this.avatar,
				nickname: this.nickname,
				information: this.information,
				lon: this.lon,
				lat: this.lat,
				address: this.address
			}).then(res => {
				uni.showToast({
					title: res.msg
				});
				this.getInfo();
				this.fileList1 = [];
			});
		},
		getInfo() {
			this.https('/order/getUserinfo', {}).then(res => {
				this.userinfo = res.data;
				this.avatar = res.data.avatar;
				this.nickname = res.data.nickname;
				this.information = res.data.information;
				this.lat = res.data.lat;
				this.lon = res.data.lon;
				this.address = res.data.address;
				uni.setStorageSync('userinfo', res.data);
			});
		},
		changeAvatar() {
			let _this = this;
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album', 'camera'], //从相册选择或者打开相机
				success: function(res) {
					uni.uploadFile({
						url: 'https://chazhichashi.rchz.top/api/newupload/postImage',
						filePath: res.tempFilePaths[0],
						name: 'file',
						success: uploadFileRes => {
							let data = JSON.parse(uploadFileRes.data);
							console.log(data);
							_this.userinfo.avatar = data.data;
							_this.avatar = data.data;
						}
					});
				}
			});
		},
		changeName() {
			console.log('11111');
			this.show = true;
		},
		confirm() {
			this.show = false;
			this.userinfo.nickname = this.nickname;
		},
		cancel() {},
		loginOut() {
			uni.showModal({
				content: '您确定要退出吗？',
				success(res) {
					if (res.confirm) {
						uni.clearStorage();
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}, 700);
					}
				}
			});
		}
	}
};
</script>

<style lang="scss">
.geren {
	padding: 32rpx;
	.imgs {
		margin-top: 32rpx;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		image {
			width: 200rpx;
		}
	}
}
.username {
	display: flex;
	width: 670rpx;
	justify-content: space-between;
	margin: 0 auto;
	height: 100rpx;
	align-items: center;
	border-bottom: solid 2rpx #dde0e4;
	view:nth-child(1) {
		color: #000000;
		font-size: 28rpx;
	}
	view:nth-child(2) {
		color: #666666;
		font-size: 28rpx;
	}
	.avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
	}
	image {
		width: 14rpx;
		height: 28rpx;
	}
}
.btn {
	display: flex;
	justify-content: space-between;
	position: fixed;
	width: 100%;
	bottom: 100rpx;
}
.btn button {
	width: 240rpx;
	height: 80rpx;
	background: #45bb52;
	border-radius: 16rpx;
	color: #fff;
	font-size: 32rpx;
	line-height: 80rpx;
}
</style>
